from google.appengine.ext import webapp
from google.appengine.api import users
from google.appengine.ext.webapp import template
from DataClass.WebsiteRec import WebsiteRec
from DataClass.FileData import FileData

import os
import cgi

class FileHandler(webapp.RequestHandler):
  def get(self):
#        title = self.request.get('title')
#        movie = getMovie(title)
#        if (movie and movie.picture):
#            self.response.headers['Content-Type'] = 'image/jpeg'
#            self.response.out.write(movie.picture)
#        else:
#            self.redirect('/static/noimage.jpg')

    querystring=cgi.FieldStorage()

    filename=querystring.getvalue('filename')
    Website = WebsiteRec.GetCurrentWebsite(self.request)
    file = None

    if Website and filename and (len(filename) > 0):
        file = FileData.GetFileBySiteNameAndFileName(Website.sitename, filename)

    if file:
        self.response.headers['Content-Type'] = file.mimetype
        self.response.out.write(file.bytes)
    else:
        self.redirect('/static/noimage.jpg')
         
